#include "main.h"
#include "btgame.h"
#include "object.h"
#include "res.h"

int  BTGame_C::Init()
{
	m_GState = GST_START;

	m_Map.Init();
	m_Role.Init();

	return 1;
}

void BTGame_C::Update(float dt)
{
	m_StateTime += dt;

	switch(m_GState)
	{
	case GST_START:
		if( m_StateTime > 0.5f )
		{
			m_GState = GST_PLAY;
			m_StateTime = 0;
		}
		break;

	case GST_PLAY:
		if( m_Map.Update(dt,m_Role) == STATE_PLAY)
			m_Role.Update(dt);
		if( m_Role.m_bDead )
		{
			m_GState = GST_FAIL;
			m_StateTime = 0;
		}
		else if( m_Map.Sucess() )
		{
			m_GState = GST_WIN;
			m_StateTime = 0;
		}
		break;

	case GST_WIN:
	case GST_FAIL:
		if( hge->Input_KeyDown(HGEK_ENTER) )
		{
			Exit();
			Init();
			m_StateTime = 0;
		}
		break;

	default:
		break;
	}

}

void BTGame_C::Draw()
{
	m_Role.Draw();
	m_Map.Draw();

	switch(m_GState)
	{
	case GST_START:
		fnt->printf(SCREEN_WIDTH/2.0f,SCREEN_HEIGHT/2.0f,HGETEXT_CENTER,"Here we go!");
		break;

	case GST_PLAY:
		break;

	case GST_WIN:
		if( (int)(m_StateTime/0.2f)%2 == 0 )
			sprYeah->Render(SCREEN_WIDTH/2.0f,SCREEN_HEIGHT/2.0f);
		else
			sprYeah->RenderEx(SCREEN_WIDTH/2.0f,SCREEN_HEIGHT/2.0f,0.15f);
		fnt->printf(SCREEN_WIDTH/2.0f,SCREEN_HEIGHT/2.0f-sprYeah->GetHeight()/2,HGETEXT_CENTER,"You Win!!\nPress ENTER to play again.");
		break;

	case GST_FAIL:
		sprFail->Render(SCREEN_WIDTH/2.0f,SCREEN_HEIGHT/2.0f);
		fnt->printf(SCREEN_WIDTH/2.0f,SCREEN_HEIGHT/2.0f-sprFail->GetHeight()/2,HGETEXT_CENTER,"You Dead!!\nPress ENTER to try again.");
		break;

	default:
		break;
	}		
}

void BTGame_C::Exit()
{
	m_Map.Exit();
}